接下來要開始引用訓練模組了,這次打算沿用關閉叉叉的gui和controller,並且把主程式替換成eliminate_block.py
from gui import GUI_Handler
from game_controller import Game_Controller
from queue import Queue
from threading import Thread
from time import sleep
from keras.models import load_model
from PIL import Image, ImageOps, ImageGrab
import cv2
import numpy as np
import enum
class Application(Thread):
range_x1:int
range_y1:int
range_x2:int
range_y2:int
# 對螢幕的尺寸修正
window_rate_x = 1920 / 1920
window_rate_y = 1080 / 1080
grid_size = 7
block_size_x = 0
block_size_y = 0
block_type = None
block_position = None
def __init__(self, gui_rsp, cont_rsp) -> None:
Thread.__init__(self)
self.q_gui_rsp = gui_rsp
self.q_cont_rsp = cont_rsp
self.keras_init()
def keras_init(self):
# Load the model
self.model = load_model('keras_model/block.h5')
self.data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
# 預處理
self.model.predict(self.data)
def fix_range(self):
# 取得範圍長寬
pass
def block_detect(self):
# 辨識版面上的方塊種類
pass
def run(self):
while True:
if not self.q_gui_rsp.empty():
msg = self.q_gui_rsp.get(True, 0.5)
self.handle_gui_msg(msg)
if not self.q_cont_rsp.empty():
msg = self.q_cont_rsp.get(True, 0.5)
self.handle_cont_msg(msg)
sleep(0.1)
def handle_gui_msg(self, msg):
global gui, cont
if msg == 'set_range':
cont.game_listen()
elif msg == 'auto_play':
pass
def handle_cont_msg(self, msg):
global gui, cont
if type(msg) == list:
if msg[0] == 'on_click':
# print(msg)
if msg[1] == 1:
self.range_x1, self.range_y1 = msg[2], msg[3]
elif msg[1] == 2:
self.range_x2, self.range_y2 = msg[2], msg[3]
cont.game_listen_stop()
self.fix_range()
gui.set_range_result(self.range_x1, self.range_y1, self.range_x2, self.range_y2)
class Color(enum.Enum):
blue = 0
red = 1
orange = 2
purple = 3
yellow = 4
green = 5
if __name__ == '__main__':
q_gui_rsp = Queue()
q_cont_rsp = Queue()
app = Application(q_gui_rsp, q_cont_rsp)
cont = Game_Controller(q_cont_rsp)
app.start()
gui = GUI_Handler(q_gui_rsp)
gui.window.mainloop()
大部分的東西都跟關閉廣告差不多,裡面被我寫pass的就是日後要新增的功能
接著改一下gui畫面,需要多一個auto_play按鈕
# def item_init(self):
self.button_auto_play = Button(text="auto play", font=("Arial", 14, "bold"), padx=5, pady=5, bg="blue", fg="light green", command=self.auto_play)
self.button_auto_play.pack()
def auto_play(self):
self.q_gui_rsp.put("auto_play")
self.button_auto_play['text'] = 'playing'
self.button_auto_play['state'] = 'disable'
def auto_play_finish(self):
self.button_auto_play['text'] = 'auto play'
self.button_auto_play['state'] = 'normal'
明天開始引用訓練模組